<?php

class TplUserCatalog
{
	public static function TopMenu($tit=false)
	{
		$GLOBALS['jscripts'][]='js/module_catalog.js';
		$GLOBALS['jscripts'][]='addons/colorbox/jquery.colorbox-min.js';
		$GLOBALS['jscripts'][]='js/color.js';
		$GLOBALS['head']['colorbox']='<link rel="stylesheet" media="screen" href="addons/colorbox/colorbox.css">';
		#CatalogCron
		if(isset(Eleanor::$services['cron']))
		{
			$task=Eleanor::$Cache->Get($GLOBALS['Eleanor']->module['config']['n'].'_nextrun');
			$t=time();
			$task=$task===false && $task<=$t ? '<img src="'.Eleanor::$services['cron']['file'].'?'.Url::Query(array('module'=>$GLOBALS['Eleanor']->module['name'],'language'=>Language::$main==LANGUAGE ? false : Language::$main,'rand'=>$t)).'" style="width:1px;height1px;" alt="">' : '';
		} else $task='';
		#[E] CatalogCron
		// if(!empty($GLOBALS['Eleanor']->module['general']))
			// return$task;
		if (count($_GET) <= 1 || isset($_GET['sort'])) {
            $lang = Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
			return '<div id="document_body_center_menu_filter_contatiner">
					<div id="document_body_center_menu_filter_text">'. $lang['sort'] .'</div>
					<div id="document_body_center_menu_filter_buttons">
					<a id="button_first" class="document_body_center_menu_filter_button" href="'. $GLOBALS['Eleanor']->Url->Construct(array('sort' => array('sort' => 'price'))) .'" title="'. strip_tags(['sort_price']) .'"></a>
					<a id="button_second" class="document_body_center_menu_filter_button" href="'. $GLOBALS['Eleanor']->Url->Construct(array('sort' => array('sort' => 'view'))) .'" title="'. strip_tags($lang['sort_view']) .'"></a>
					<a id="button_third" class="document_body_center_menu_filter_button" href="'. $GLOBALS['Eleanor']->Url->Construct(array('sort' => array('sort' => 'alpha'))) .'" title="'. strip_tags($lang['sort_alpha']) .'"></a>
					</div>
				</div>
				<div id="document_body_center_menu_records_container">';
		}
	}

	private static function NList($data) {
		if (!isset($GLOBALS['head']['colorbox'])) {
			$GLOBALS['head']['colorbox']='<link rel="stylesheet" media="screen" href="addons/colorbox/colorbox.css" />';
			$GLOBALS['head'][]='<script>/*<![CDATA[*/
			$(document).ready(function(){
			$(".gallery").colorbox({rel:"gallery"});
			});/*]]*/></script>';
		}
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$T = clone Eleanor::$Template;

		foreach($data['items'] as $v) {
			$T->BaseCatalog(array(
				'readmore'=>'<a href="'.$v['_url'].'#more">'. $lang['open'] .'</a>',
				'review'=> sprintf($lang['reads_'], $v['reads']),
				'title'=> $v['title'],
				'price'=> sprintf($lang['price'], $v['price']),
				'image'=> $v['image'] ? $v['image'] : '/templates/AutoDiagnostika/images/main_record_image.png',
				// 'price'=> $langs['open'],
			));
		}
		return$T.'</div><script>//<![CDATA[
$(function(){
	CORE.Catalog.Init("article.jaunumi_main_article","'.$GLOBALS['Eleanor']->module['name'].'");
})//]]></script>';
	}

	public static function CatalogList($data,$cnt,$page, $pages, $pp, $links) {
		return self::TopMenu().self::NList($data).Eleanor::$Template->Pages($cnt, $pp, $page, array($links['pages'],$pages=>$links['first_page']));
	}

	public static function CatalogDateList($date,$data,$cnt,$page,$pp)
	{
		return self::TopMenu(reset($GLOBALS['title'])).self::NList($data).Eleanor::$Template->Pages($cnt,$pp,-$page,$GLOBALS['Eleanor']->Url->Construct(array('do'=>$date,array('page'=>'{page}')),true,''));
	}

	public static function CatalogCategoryList($category,$data,$cnt,$page,$pp)
	{
		$cu=$GLOBALS['Eleanor']->Categories->GetUrl($category['id']);
		return self::ShowCategories($category['id']).self::NList($data)
			.Eleanor::$Template->Pages($cnt,$pp,-$page,$GLOBALS['Eleanor']->Url->Construct($cu+array('page'=>array('page'=>'{page}')),true,$GLOBALS['Eleanor']->Url->delimiter));
	}

	public static function ShowCategories($cat=0)
	{
		if(isset($GLOBALS['Eleanor']->Categories->dump[$cat]))
		{
			$way=$GLOBALS['Eleanor']->Categories->dump[$cat]['parents'] ? explode(',',rtrim($GLOBALS['Eleanor']->Categories->dump[$cat]['parents'],',')) : array();
			foreach($way as $k=>&$v)
				if(isset($GLOBALS['Eleanor']->Categories->dump[$v]))
					$v=array(
						$GLOBALS['Eleanor']->Url->Construct($GLOBALS['Eleanor']->Categories->GetUrl($v),true,$GLOBALS['Eleanor']->Url->delimiter),
						$GLOBALS['Eleanor']->Categories->dump[$v]['title'],
					);
				else
					unset($way[$k]);
			if($way)
			{
				$w='<span class="cat1">';
				foreach($way as $v)
					$w='<a href="'.$v[0].'">'.$v[1].'</a> &raquo; ';
				$w.='</span><hr />';
			}
			else
				$w = '';
			$c = $w .'<table style="width:100%"><tr>'
				.($GLOBALS['Eleanor']->Categories->dump[$cat]['image'] ? '<td><img src="'.$GLOBALS['Eleanor']->Categories->imgfolder.$GLOBALS['Eleanor']->Categories->dump[$cat]['image'].'" alt="'.$GLOBALS['Eleanor']->Categories->dump[$cat]['title'].'" title="'.strip_tags($GLOBALS['Eleanor']->Categories->dump[$cat]['title']).'" /></td>' : '')
				.'<td><td style="width:100%"><h2 class="title">'.$GLOBALS['Eleanor']->Categories->dump[$cat]['title'].'</h2>'.$GLOBALS['Eleanor']->Categories->dump[$cat]['description'].'</td></tr></table>';
		}
		return self::TopMenu().Eleanor::$Template->OpenTable().$c.Eleanor::$Template->CloseTable();
	}

	public static function ShowCatalog($a, $comments, $image) {
		$GLOBALS['jscripts'][]='//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js';
		$GLOBALS['jscripts'][]='/js/slaider.js';
		$GLOBALS['jscripts'][]='/js/tabs.js';
		if (!isset($GLOBALS['head']['colorbox'])) {
//			$GLOBALS['jscripts'][]='/js/color.js';
			$GLOBALS['head']['colorbox']='<link rel="stylesheet" media="screen" href="/addons/colorbox/colorbox.css">';
		}
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];

		$GLOBALS['Eleanor']->Url->hash='more';

		return self::TopMenu(false)
			.Eleanor::$Template->BaseCatalogOne(array(
				'reads' => $lang['reads'] .' <span>'. $a['reads']. '</span>',
				'price' => '<div id="aprikojuma_katalogs_preces_cena">'. $lang['price_one'] .':<div id="aprikojuma_katalogs_preces_skaitlis">'. $a['price'] .'</div>Ls</div>',
				'comments' => $comments,
				'deliver' => $lang['deliver'],
				'description' => $lang['description'],
				'deliver_text' => $lang['deliver_text'],
				'reviews' => $lang['reviews'],
				'information' => $lang['information'],
				'title'=>$a['title'],
				'text'=>$a['text'],
				'info'=>$a['info'],
				'advertisement'=>$a['advertisement'],
				'images' => $image ? $image : array(array('/templates/AutoDiagnostika/images/main_record_image.png')),
			));
	}
}